package com.baizhi.controller;

import com.baizhi.dto.UserPageDTO;
import com.baizhi.po.UserPO;
import com.baizhi.po.UserPagePO;
import com.baizhi.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户(User)表控制层
 *
 * @author makejava
 * @since 2021-09-24 00:13:06
 */
@RestController
@RequestMapping("users")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @GetMapping
    public UserPagePO queryPage(@RequestParam("token") String token, @RequestParam("page") Integer page,
                                @RequestParam("per_page") Integer per_page, @RequestParam(value = "id", required = false) Integer id,
                                @RequestParam(value = "name", required = false) String name, @RequestParam(value = "phone", required = false) String phone
    ) {
        UserPageDTO userPageDTO = new UserPageDTO(token, page, per_page, id, name, phone);
        UserPagePO userPagePO = userService.queryPage(userPageDTO);
        return userPagePO;
    }

    @GetMapping("/{id}")
    public UserPO queryById(@PathVariable("id") Integer id) {
        UserPO userPO = userService.queryById(id);
        return userPO;
    }

    @GetMapping("/query/{name}")
    public UserPO queryByName(@PathVariable("name") String name) {
        UserPO userPO = userService.queryByName(name);
        return userPO;
    }
}

